﻿Imports System.Linq



Public Class Linq_Product
    Public Function createProduct(ByVal prNaam As String, ByVal prPrijs As Decimal, ByVal prOmschrijving As String) As Boolean
        Dim dbc = New SaktieDBDataContext()

        Dim inserted As Boolean = False

        Dim myProduct As New Product

        myProduct.product_naam = prNaam
        myProduct.prijs = prPrijs
        myProduct.product_omschrijving = prOmschrijving

        dbc.Products.InsertOnSubmit(myProduct)
        dbc.SubmitChanges()
        inserted = True

        Return inserted
    End Function

    Public Function getAlleProducten() As List(Of ProductBean)
        Dim SaktieDBDataContext1 = New SaktieDBDataContext()
        Dim producten As New List(Of ProductBean)
        Dim ProductQuery = From Product In SaktieDBDataContext1.Products
              Select Product

        For Each p In ProductQuery

            Dim objProduct = New ProductBean()
            objProduct.Naam = p.product_naam.Trim
            objProduct.Omschrijving = p.product_omschrijving.Trim
            objProduct.prodID = p.id
            objProduct.Prijs = p.prijs
            producten.Add(objProduct)
        Next
        Return producten

    End Function

    Public Function getProductByID(ByVal pId As Integer) As ProductBean
        Dim SaktieDBDataContext1 = New SaktieDBDataContext()
        Dim ProductQuery = From Product In SaktieDBDataContext1.Products
                            Select Product
                            Where Product.id = pId
                            Take 1

        Dim myProduct As New ProductBean
        myProduct.Naam = ProductQuery.First.product_naam
        myProduct.Omschrijving = ProductQuery.First.product_omschrijving
        myProduct.prodID = ProductQuery.First.id
        myProduct.Prijs = ProductQuery.First.prijs
        Return myProduct

    End Function

    Public Function getProductId(ByVal prodNaam As String, ByVal prodPrijs As String) As Integer
        Dim SaktieDBDataContext1 = New SaktieDBDataContext()

        Dim ProductQuery = From Product In SaktieDBDataContext1.Products
                            Where Product.product_naam = prodNaam.Trim And Product.prijs = prodPrijs
                            Select Product.id

        Return ProductQuery.First
    End Function


End Class
